<template>
	<view class="page-wrap">
		<view class="uni-container">
			<view class="headList">

				<view class="example-body">
					<uni-row class="example-uni-row">
						<uni-col :span="16">
							<uni-search-bar class="uni-mt-10" radius="5" placeholder="企业名搜索" clearButton="none"
								cancelButton="none" v-model="searchValue" @confirm="handleSearch" />
						</uni-col>
						<uni-col :span="4">
							<view>
								<button class="search-btn" @click="handleSearch">查询</button>

							</view>

						</uni-col>
						<uni-col :span="4">
							<view>
								<button class="search-btn" @click="handleCreate(item)">创建工单</button>
							</view>
						</uni-col>

					</uni-row>
				</view>

				<uni-row class="example-uni-row">
					<uni-col :span="12">
						<uni-datetime-picker v-model="selectedTime" placeholder="生成时间" type="date" :clear-icon="true"
							fields="day" @change="timeChange"></uni-datetime-picker> </uni-col>
					<uni-col :span="12">
						<uni-data-select v-model="selectedRange" @clear="clear" placeholder="经营范围" :localdata="range"
							@change="rangeChange"></uni-data-select>
					</uni-col>

				</uni-row>
			</view>
			<uni-table width="100%"  ref="table" :loading="loading" border stripe  emptyText="暂无更多数据">
				<uni-tr>
					<uni-th width="100" align="center">企业名称</uni-th>
					<uni-th width="150" align="center">工单名称</uni-th>
					<uni-th align="center">工单状态</uni-th>
					<uni-th width="150" align="center">时间</uni-th>
					<uni-th align="center">操作</uni-th>
				</uni-tr>
				<uni-tr v-for="(item, index) in tableData" :key="index">
					<uni-td align="center">{{ item.operatorName }}</uni-td>
					<uni-td align="center">{{ getOrderTypeText(item.orderCategory) }}</uni-td>
					<uni-td align="center">{{ auditStatusMap[item.auditStatus] }}</uni-td>
					<uni-td align="center">{{ item.updateDate }}</uni-td>
					<uni-td>
						<view class="uni-group">


							<button v-if="item.auditStatus==0" class="uni-button" size="mini" type="primary"
								@click="handleEdit(item)">继续处理</button>
							<button class="uni-button" size="mini" type="default"
								@click="handleSelect(item)">查看详情</button>


						</view>
					</uni-td>
				</uni-tr>
			</uni-table>
			<view class="uni-pagination-box">
				<uni-pagination show-icon :page-size="queryParams.limit" :current="queryParams.page" :total="total"
					@change="handlePageChange" />
			</view>


		</view>
	</view>
</template>

<script>
	import {
		listByPage,
		getMedicalDict,
		getMedicalDictAll
	} from '@/api/order'


	export default {
		data() {
			return {
				selectedTime: null, // 新增时间选择绑定值
				selectedRange: null,
				// 新增搜索值
				searchValue: '',
				orderType: '',
				tableData: [],
				total: 0,
				loading: false,
				queryParams: {
					page: 1,
					limit: 14
				},
				auditStatusMap: {
					'0': '未完成',
					'1': '已完成'
				},
				range: [],
				reportItem: [],

			}
		},
		onLoad() {
			this.getList()
			this.getMedicalDictAll()
			this.getMedicalDict("order_type")

		},
		onShow() {
			this.getList()
		},
		/*
		 onBackPress() { // 监听物理返回键
		 this.$tab.reLaunch('/pages/work/index')
		  },
		  */
		methods: {
			// 新增匹配方法
			getOrderTypeText(value) {
				const found = this.reportItem.find(item => item.value == value)
				return found ? found.text : value // 未找到时显示原始值
			},
			// 时间选择变化
			timeChange(e) {

				this.handleSearch();
			},
			rangeChange(e) {
				this.orderType = e + "";
				this.handleSearch();
			},
			// 新增搜索处理方法
			handleSearch() {
				this.queryParams.page = 1 // 重置页码到第一页
				this.getList()
			},
			async getList() {
				this.loading = true
				try {
					const res = await listByPage({
						page: this.queryParams.page,
						limit: this.queryParams.limit,
						operatorName: this.searchValue, // 新增查询参数
						orderType: this.orderType,
						createDate: this.selectedTime
					})

					if (res.code === 0) {
						this.tableData = res.data.records
						this.total = res.data.totalRow
						console.log(this.tableData)
					}
				} catch (e) {
					console.error('请求失败:', e)
				} finally {
					this.loading = false
				}
			},

			handlePageChange(e) {
				// 从 e.detail 中获取当前页码
				this.queryParams.page = e.current
				this.getList()
			},


			//获取经营范围
			async getMedicalDict(type) {
				try {
					const res = await getMedicalDict(type)

					// 增加响应状态判断
					if (res.code === 0) {
						if (type === 'order_type') {
							this.range = res.data.map(item => ({
								text: item.label,
								value: item.value
							}))
						} else if (type === 'food_operate_type,medical_operate_type,food_production_type') {
							this.reportItem = res.data.map(item => ({
								text: item.label,
								value: item.value
							}))
						}

					} else {
						throw new Error(res?.data?.msg || '接口返回异常')
					}
				} catch (error) {
					uni.showToast({
						title: error.message || '获取失败',
						icon: 'none',
						duration: 2000
					})
				}

			},

			async getMedicalDictAll() {
				try {
					const res = await getMedicalDictAll("food_operate_type,medical_operate_type,food_production_type")
					// 增加响应状态判断
					if (res.code === 0) {
						this.reportItem = res.data.map(item => ({
							text: item.label,
							value: item.value
						}))

					} else {
						throw new Error(res?.data?.msg || '接口返回异常')
					}
				} catch (error) {
					uni.showToast({
						title: error.message || '获取失败',
						icon: 'none',
						duration: 2000
					})
				}
			},
			//编辑工单
			handleEdit(item) {
				this.navigatorToReport(item.orderCategory, item.id, 'EDIT', item)
			},
			//创建工单
			handleCreate(item) {
				this.$tab.navigateTo('/pages/work/create/index')
			},
			//查看工单
			handleSelect(item) {
				this.navigatorToReport(item.orderCategory, item.id, 'FALSE', item)
			},
			//跳转工单
			navigatorToReport(orderCategory, orderId, editStatus, item) {

				switch (orderCategory) {
					case '0':
						uni.navigateTo({
							url: `/pages/work/create/food-business/business-work-rule?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;
					case '1': //第二类 食品经营许可现场核查内容
						uni.navigateTo({
							url: `/pages/work/create/food-business/checklist-fifth?workOrderId=${encodeURIComponent(orderId)}&orderCategory=${encodeURIComponent('1')}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;
					case '2': //第五类 食品经营许可现场核查内容
						uni.navigateTo({
							url: `/pages/work/create/food-business/checklist-fifth?workOrderId=${encodeURIComponent(orderId)}&orderCategory=${encodeURIComponent('2')}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;
					case '3': //食品经营许可技术审评报告
						uni.navigateTo({
							url: `/pages/work/create/food-business/report-technical?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;
					case '4': //企业主动撤回
						uni.navigateTo({
							url: `/pages/work/create/food-business/enterprise-withdraw?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;
					case '5': //医疗器械经营质量管理规范现场检查表
						uni.navigateTo({
							url: `/pages/work/create/medical/medical-checklist?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;
					case '6': //医疗器械经营质量管理规范现场检查报告
						uni.navigateTo({
							url: `/pages/work/create/medical/medical-review?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;
					case '7': //医疗器械经营技术审查报表
						uni.navigateTo({
							url: `/pages/work/create/medical/medical-checkReport?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;
					case '8': //企业负责人现场考核
						uni.navigateTo({
							url: `/pages/work/create/medical/medical-assessor?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}&qualityManagerCertNo=${encodeURIComponent(item.qualityManagerCertNo)}&qualityManagerName=${encodeURIComponent(item.qualityManagerName)}`
						});
						break;
					case '9': //跳转到医疗器械-企业主动撤回
						uni.navigateTo({
							url: `/pages/work/create/medical/medical-reviewCheck?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;

					case '11': //跳转到食品生产-人员派选
						uni.navigateTo({
							url: `/pages/work/create/food-production/check-assign?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;
					case '12': //珠海市食品药品(医疗器械)审评认证中心 食品生产许可现场核查通知书

						uni.navigateTo({
							url: `/pages/work/create/food-production/check-notice?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;

					case '13': //食品、食品添加剂生产许可现场核查首次会议签到表

						uni.navigateTo({
							url: `/pages/work/create/food-production/first-meeting?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;
					case '15':
						uni.navigateTo({
							url: `/pages/work/create/food-produce/index?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}&orderCategory=15`
						});
						break;
						
						case '16': //食品、食品添加剂生产许可现场核查末次会议签到表
						
							uni.navigateTo({
								url: `/pages/work/create/food-production/last-meeting?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
							});
							break;
					case '17': //食品技术审评报告
						uni.navigateTo({
							url: `/pages/work/create/food-production/checkReport?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;

					case '18': //广东省食品生产许可核查人员廉洁自律承诺书
						uni.navigateTo({
							url: `/pages/work/create/food-production/commit-letter?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;

					case '19': //食品生产许可现场核查人员工作纪律反馈单
						uni.navigateTo({
							url: `/pages/work/create/food-production/feedback?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
						});
						break;
						
					case '20': //食品生产 撤销审查
							uni.navigateTo({
								url: `/pages/work/create/food-production/enterprise-withdraw?workOrderId=${encodeURIComponent(orderId)}&editStatus=${encodeURIComponent(editStatus)}`
					});
					break;
					default:
						return;
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.page-wrap {
		padding: 20rpx;
		background-color: #ffffff;
	}

	.uni-container {
		background: #fff;
		border-radius: 8rpx;
		overflow: hidden;
	}

	.uni-group {
		display: flex;
		justify-content: center;
		gap: 10rpx;
	}

	.uni-button {
		flex: 1;
		padding: 0 10rpx;
	}

	.uni-pagination-box {
		padding: 20rpx 0;
		margin-bottom: 30px;
	}

	.example-uni-row {
		padding: 9px;

	}

	.search-btn {
		margin-top: 10px;
		height: 35rpx;
		line-height: 70rpx;
		font-size: 15rpx;
		padding: 0 20rpx;
		margin-left: 5px;
		background: #007AFF;
		color: white;
		border-radius: 8rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>